#include "pthread.h"
#include "stdio.h"
#include "debug.h"

#include "ipport_com.h"
#include "socket.h"

void* app_thread_entry(void *p)
{
    int sockfd;
    struct sockaddr sockaddr;
    socklen_t socklen;
    char buf[128];
    ssize_t size;
    
#if USING_IPPORT_COM
    ASSERT_KERNEL(ipport_com_configuration() == HZ_EOK);
#endif
    
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
    {
        printf("init socket error.\r\n");
    }
    
    sockaddr.sin_port = 4300;
    sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    bind(sockfd, &sockaddr, 1);
    listen(sockfd, 0);
    accept(sockfd, &sockaddr, &socklen);
    
    while(1)
    {
       if ((size = read(sockfd, buf, 128)) > 0)
       {
           printf("recv [%d], %s.\r\n", size, buf);
       }
    }
}

err_t app_init(void)
{  
/*  
    int err;
    int tid;
    pthread_attr_t attr;
    sched_param_t app_sched_param =
      SCHED_PARAM_INIT(PTHREAD_TYPE_USER,
                       2,
                       12);
    
    pthread_attr_setschedparam(&attr, &app_sched_param);
    pthread_attr_setstacksize(&attr, 1024);
    
    err = pthread_create(&tid,
                         &attr,
                         app_thread_entry,
                         NULL);
    HZ_ASSERT_KERNEL(err == HZ_EOK);
*/
  
    return 0;
}